<html>
<head><meta charset="utf-8"><title>Closure annotation to satisfy FnOnce FnMut · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html">Closure annotation to satisfy FnOnce FnMut</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240264316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240264316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240264316">(May 25 2021 at 23:46)</a>:</h4>
<p>I'm a beginner and confused why this seemingly-unrelated annotation fixes this <code>FnOnce</code> compilation error.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">sum_of_multiples</span><span class="p">(</span><span class="n">limit</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="n">factors</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u32</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Fails to compile:</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">is_multiple</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">factors</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">any</span><span class="p">(</span><span class="o">|</span><span class="n">y</span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="c1">// Fixed with annotation:</span>
<span class="w">    </span><span class="c1">// let is_multiple = |x: &amp;u32| factors.iter().any(|y| x % y == 0);</span>

<span class="w">    </span><span class="p">(</span><span class="mi">1</span><span class="o">..=</span><span class="n">limit</span><span class="p">).</span><span class="n">filter</span><span class="p">(</span><span class="n">is_multiple</span><span class="p">).</span><span class="n">sum</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>3  |       let is_multiple = |x| factors.iter().any(|y| x % y == 0);
   |                         --------------------------------------
   |                         |
   |                         doesn&#39;t satisfy `&lt;_ as FnOnce&lt;(&amp;u32,)&gt;&gt;::Output = bool`
   |                         doesn&#39;t satisfy `_: FnMut&lt;(&amp;u32,)&gt;`
</code></pre></div>
<p>Still not sure what that annotation has to do with satisfying <code>FnOnce</code> / <code>FnMut</code>. Read though both these sections again too:<br>
<a href="https://doc.rust-lang.org/rust-by-example/fn/closures/input_parameters.html?highlight=fnonce#as-input-parameters">https://doc.rust-lang.org/rust-by-example/fn/closures/input_parameters.html?highlight=fnonce#as-input-parameters</a><br>
<a href="https://doc.rust-lang.org/book/ch13-01-closures.html#capturing-the-environment-with-closures">https://doc.rust-lang.org/book/ch13-01-closures.html#capturing-the-environment-with-closures</a></p>
<p>Guessing I should be more focused on the <code>&amp;u32</code> part of the errors, rather than the <code>FnOnce</code> part.</p>



<a name="240264633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240264633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240264633">(May 25 2021 at 23:51)</a>:</h4>
<p>The syntax <code>x % y</code> desugars to <code>x.mod(y)</code>, and if the only way you use a variable is to call a method on it rust usually can't infer the type. I'm not sure to what extent rust can infer the type in the closure from the trait bound in <code>filter()</code></p>



<a name="240264865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240264865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240264865">(May 25 2021 at 23:54)</a>:</h4>
<p>Note that rust <em>can</em> infer the type if you inline <code>is_multiple</code>, which makes me think it has something to do with the lifetime arguments, since they are generalized differently for standalone closures compared to direct function arguments</p>



<a name="240265552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240265552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240265552">(May 26 2021 at 00:06)</a>:</h4>
<p>If you can inline a closure, it's far better to do so.</p>



<a name="240265574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240265574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240265574">(May 26 2021 at 00:06)</a>:</h4>
<p>The compiler plays tricks when a closure is passed to the usage site and can more strongly perform type inference.</p>



<a name="240265600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240265600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240265600">(May 26 2021 at 00:07)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/41078">https://github.com/rust-lang/rust/issues/41078</a></p>



<a name="240265617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240265617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240265617">(May 26 2021 at 00:07)</a>:</h4>
<p><a href="https://stackoverflow.com/q/36415348/155423">https://stackoverflow.com/q/36415348/155423</a></p>



<a name="240273294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240273294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240273294">(May 26 2021 at 02:32)</a>:</h4>
<p>Inlining the closures helps cut down on type annotations. Now I'm confused about how these are the same:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="n">limit</span><span class="p">).</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">factors</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">any</span><span class="p">(</span><span class="o">|</span><span class="n">d</span><span class="o">|</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)).</span><span class="n">sum</span><span class="p">()</span><span class="w"></span>
<span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="n">limit</span><span class="p">).</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">factors</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">any</span><span class="p">(</span><span class="o">|</span><span class="n">d</span><span class="o">|</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w">  </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w">  </span><span class="n">d</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)).</span><span class="n">sum</span><span class="p">()</span><span class="w"></span>
</code></pre></div>
<p>I'm guessing this is due to auto-dereferencing. Not sure why dereferencing is mandatory for <code>*d != 0</code>, but optional for <code>*x % *d</code>.</p>



<a name="240274150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240274150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240274150">(May 26 2021 at 02:50)</a>:</h4>
<p>operator type matching seems to be more stringent than that of the corresponding trait</p>



<a name="240275235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240275235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240275235">(May 26 2021 at 03:15)</a>:</h4>
<p><span class="user-mention" data-user-id="263479">@miles</span> It's not autodereferencing, <a href="https://doc.rust-lang.org/std/primitive.u32.html#impl-Rem%3C%26%27_%20u32%3E-1"><code>&amp;u32</code> implements <code>Rem&lt;&amp;u32&gt;</code></a></p>



<a name="240275319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240275319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240275319">(May 26 2021 at 03:18)</a>:</h4>
<p>the arithmetic binary operator implementations have a <code>forward_ref_binop!</code> macro that i haven't been able to find yet</p>



<a name="240275378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240275378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240275378">(May 26 2021 at 03:19)</a>:</h4>
<p><a href="https://doc.rust-lang.org/src/core/internal_macros.rs.html#28">https://doc.rust-lang.org/src/core/internal_macros.rs.html#28</a></p>



<a name="240277228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240277228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240277228">(May 26 2021 at 04:01)</a>:</h4>
<p>Is it possible to use the <a href="https://doc.rust-lang.org/std/primitive.u32.html#method.rem">.rem()</a> method directly?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span>:<span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">).</span><span class="n">rem</span><span class="p">(</span><span class="o">&amp;</span><span class="mi">4</span><span class="p">);</span><span class="w"> </span><span class="c1">// Why doesn't this work?</span>
<span class="kd">let</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>Would it be better zulip etiquette for me to create a new topic? Seems like this question is still somewhat related to the thread's existing discussion.</p>



<a name="240277298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240277298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240277298">(May 26 2021 at 04:02)</a>:</h4>
<p>It's a trait function so you need the trait in scope with <code>use std::ops::Rem;</code></p>



<a name="240277339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240277339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240277339">(May 26 2021 at 04:03)</a>:</h4>
<p>alternatively you can not import it but use <code>std::ops::Rem::rem(&amp;x, &amp;4)</code> instead</p>



<a name="240277347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240277347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240277347">(May 26 2021 at 04:04)</a>:</h4>
<p><code>(&amp;&amp;&amp;1u32).add(1u32)</code> works, presumably due to auto-deref</p>



<a name="240277421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Closure%20annotation%20to%20satisfy%20FnOnce%20FnMut/near/240277421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Closure.20annotation.20to.20satisfy.20FnOnce.20FnMut.html#240277421">(May 26 2021 at 04:05)</a>:</h4>
<p>but <code>Add::add(&amp;&amp;&amp;1u32, 1u32)</code> doesn't, because that bypasses method receiver coercion</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>